//
//  SettingTableViewController.m
//  彩票项目
//
//  Created by 伍银荣 on 2021/8/4.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "SettingTableViewController.h"
#import "RedeemCodeViewController.h"
#import "SettingCell.h"

@interface SettingTableViewController ()
@property (nonatomic, strong) NSArray *groups;

@end

@implementation SettingTableViewController

-(instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(NSArray *)groups {
    if (!_groups) {
        _groups = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.plistName ofType:@"plist"]];
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"group = %@",self.groups);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClick)];
    
    
}

- (void)backBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
//获取组的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

//返回每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *groups = self.groups[section];
    NSArray *items = groups[@"items"];
    return items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //获取每组的信息
    NSDictionary *groupsDict = self.groups[indexPath.section];
    //获取对应组的对应行的信息
    NSDictionary *rowsDict = groupsDict[@"items"][indexPath.row];
    //NSLog(@"rowsDict = %@",rowsDict);
    
    //创建cell
    SettingCell *cell = [SettingCell setttingCellWithTableView:tableView andItem:rowsDict];
    //设置数据
    cell.item = rowsDict;
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDictionary *groupDict = self.groups[section];
    return groupDict[@"header"];
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    NSDictionary *groupDict = self.groups[section];
    return groupDict[@"footer"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //获取section组的信息
    NSDictionary *groupsDict = self.groups[indexPath.section];
    //获取到行的信息
    NSDictionary *rowDict = groupsDict[@"items"][indexPath.row];
    if (rowDict[@"targetVC"] && [rowDict[@"targetVC"] length] > 0) {
        //获取字符串类型
        NSString *targetVC = rowDict[@"targetVC"];
        //将字符串转换成类
        Class Clz = NSClassFromString(targetVC);
        UIViewController *vc = [[Clz alloc]init];
        
        if ([vc isKindOfClass:[SettingTableViewController class]]) {
            SettingTableViewController *settingVC = (SettingTableViewController *)vc;
            settingVC.plistName = rowDict[@"plistName"];
        }
        vc.navigationItem.title = rowDict[@"title"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (rowDict[@"funcName"] && [rowDict[@"funcName"] length]  > 0) {
        //把字符串转化成一个方法
        SEL funcName = NSSelectorFromString(rowDict[@"funcName"]);
        
        // 忽略警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        //执行方法
        [self performSelector:funcName];
#pragma clang diagnostic pop
    }
}

//检查版本
- (void)checkUpdate {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"操作提示" message:@"已经是最新版本" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
